import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

import '../macro.dart';

class SegmentedView extends StatelessWidget {
  final List<String> titles;
  final TextStyle? titleNormal;
  final TextStyle? titleSelected;
  final int? defaultSelectedIndex;
  final double? itemHeight;
  final double? itemSpacing;
  final IndexCallBack? onPressed;
  final Color? lineColor;
  final MainAxisAlignment mainAxisAlignment;
  final PageController? controller;

  const SegmentedView({
    super.key,
    required this.titles,
    this.itemHeight,
    this.itemSpacing = 10,
    this.titleNormal,
    this.titleSelected,
    this.defaultSelectedIndex = 0,
    this.onPressed,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.lineColor = Colors.black,
    this.controller,
  });

  @override
  Widget build(BuildContext context) {
    List<Widget> items = [];
    for (var i = 0; i < titles.length; i++) {
      items.add(
        GestureDetector(
          onTap: () {
            if (onPressed != null) {
              onPressed!(i);
            }
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.symmetric(horizontal: itemSpacing!),
            height: itemHeight,
            decoration: i == defaultSelectedIndex
                ? const BoxDecoration(
                    border: Border(
                      bottom: BorderSide(color: Colors.orange, width: 4),
                    ),
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(2.0),
                      bottomRight: Radius.circular(2.0),
                    ),
                  )
                : null,
            child: Text(
              titles[i],
              style: defaultSelectedIndex == i ? titleSelected : titleNormal,
            ),
          ),
        ),
      );
    }

    return SizedBox(
      width: Get.width,
      child: SingleChildScrollView(
        controller: controller,
        scrollDirection: Axis.horizontal,
        child: Row(
          children: items,
        ),
      ),
    );
  }
}
